leaderboardApp.controller("uploadsCtrl", function($scope, $http, notifier, httpPost) {
	$scope.loading = false;
	$scope.message = {};
	$scope.uploadedContent = {};
	$scope.dataColumns = [];
	$scope.selectedColumns = [];
	$scope.pageToReturn = "";
	$scope.insertResults = [];
	
	$scope.getLastUploadedContent = function() {
		$scope.loading = true;
		httpPost.send($scope.message, "/Leaderboard/FileUploadServlet/getLastUploadedContent", {}, function(data) {
			if(data.code === 0) {
				$scope.uploadedContent = data.uploadedContent;
				$scope.selectedColumns = new Array(data.maxColumns);
				$scope.dataColumns = data.dataColumns;
				$scope.pageToReturn = data.pageToReturn;
			}
			$scope.loading = false;
		});
	};
	
	$scope.getLastUploadedContent();
	
	
	$scope.discardRow = function(row) {
		row.discarded = true;
	};
	
	$scope.goBack = function() {
		window.location.href = $scope.pageToReturn;
	};
	
	$scope.insertRows = function() {
		$scope.loading = true;
		var discardedRows = [];
		for(var i=0; i<$scope.uploadedContent.length; i++) {
			if($scope.uploadedContent[i].discarded) {
				discardedRows.push(i);
			}
		}
		httpPost.send($scope.message, "/Leaderboard/FileUploadServlet/insertUploadedContent", 
				{"mappedColumns": $scope.selectedColumns.toString(), "discardedRows": discardedRows.toString()}, 
				function(data) {
			if(data.code == 0) {
				notifier.notifySuccess($scope.message, "Ok", data.reason);
			}
			$scope.insertResults = data.results;
			$scope.loading = false;
		});
	};
	
	$scope.classForRow = function(row, result) {
		var clase = "";
		if(row.discarded) {
			clase = "discarded";
		}
		
		if(result != null) {
			if(result.indexOf("OK") >= 0) {
				clase += "success";
			} else if(result.indexOf("ERROR") >= 0) {
				clase += "danger";
			}
		}
		
		return clase;
	};
	
	/************************************** Funciones de Utilería **************************************/
	
	$scope.hideMessage = function() {
		notifier.hideNotif($scope.message);
	};
	
	/************************************** ##################### **************************************/
});